package com.utopa.canal.client.util;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Administrator
 */
public final class CanalUtils {
    /**
     * 驼峰到下划线转换
     *
     * @param param 参数
     * @return
     */
    public static String camel2underline(String param) {
        if (StringUtils.isBlank(param)) {
            return "";
        }
        // 匹配 A-Z 之间的一个大大写字母
        Pattern p = Pattern.compile("[A-Z]");
        StringBuilder builder = new StringBuilder(param);
        Matcher mc = p.matcher(param);
        int i = 0;
        while (mc.find()) {
            builder.replace(mc.start() + i, mc.end() + i, "_" + mc.group().toLowerCase());
            i++;
        }
        if ('_' == builder.charAt(0)) {
            builder.deleteCharAt(0);
        }
        return builder.toString();
    }

    /**
     * 下划线转驼峰
     *
     * @param param
     * @return
     */
    public static String underline2camel(String param) {
        if (StringUtils.isBlank(param)) {
            return "";
        }
        // 匹配下划线小写字母
        Pattern p = Pattern.compile("_[a-z]");
        StringBuilder builder = new StringBuilder(param);
        Matcher mc = p.matcher(param.toLowerCase());
        int i = 0;
        while (mc.find()) {
            builder.replace(mc.start() - i, mc.end() - i, mc.group(0).substring(1).toUpperCase());
            i++;
        }
        return builder.toString();
    }
}
